package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookAdviceMapper;
import com.kgc.novel.domain.entity.BookAdvice;
import com.kgc.novel.domain.entity.BookClassify;
import com.kgc.novel.domain.entity.BookContent;
import com.kgc.novel.domain.entity.BookShelf;
import com.kgc.novel.domain.param.BookAdviceParam;
import com.kgc.novel.domain.vo.BookAdviceVO;
import com.kgc.novel.domain.vo.BookShelfVO;
import com.kgc.novel.service.user.BookAdviceService;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookAdviceServiceImpl extends ServiceImpl<BookAdviceMapper, BookAdvice> implements BookAdviceService {
    @Resource
    private BookAdviceMapper bookAdviceMapper;

    @Override
    public Res selectAdviceAll(BookAdviceParam bookAdviceParam) {
        List<BookAdviceVO> allShelf = bookAdviceMapper.getAllAdvice(bookAdviceParam);
        Integer countShelf = bookAdviceMapper.getCountAdvice(bookAdviceParam.getTitle(), bookAdviceParam.getCreate_time(), bookAdviceParam.getStatus());
        return Res.returnData(allShelf, (long) countShelf);
    }

    @Override
    public Res getSingleAdvice(String id) {
        BookAdvice bookShelf = bookAdviceMapper.selectById(id);
        if (bookShelf != null) {
            return Res.returnData("200", bookShelf);
        }
        return Res.returnMsg("暂无该公告的内容！");
    }

    @Override
    public Res addAdvice(BookAdvice bookAdvice) {
        LambdaQueryWrapper<BookAdvice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookAdvice::getTitle, bookAdvice.getTitle());

        BookAdvice bookAuthorObj = bookAdviceMapper.selectOne(lambdaQueryWrapper);
        if (bookAuthorObj != null) {
            return Res.returnMsg("公告已经存在，请重新添加！");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookAdvice.setId(String.valueOf(id));
        bookAdviceMapper.insert(bookAdvice);
        return Res.returnMsg("添加成功！");
    }

    @Override
    public Res updateAdvice(BookAdvice bookAdvice) {
        LambdaQueryWrapper<BookAdvice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookAdvice::getTitle, bookAdvice.getTitle());

        BookAdvice bookAdvice1 = bookAdviceMapper.selectOne(lambdaQueryWrapper);
        if (bookAdvice1 == null) {
            bookAdviceMapper.updateById(bookAdvice);
            return Res.returnMsg("修改成功！");
        }
        BookAdvice bookAdvice2 = bookAdviceMapper.selectById(bookAdvice.getId());
        if (bookAdvice2.getId().equals(bookAdvice.getId())) {
            bookAdviceMapper.updateById(bookAdvice);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnMsg("公告已经存在，请重新修改！");
    }

    @Override
    public Res deleteAdvice(String id) {
        int i = bookAdviceMapper.deleteById(id);
        if (i != -1) {
            return Res.returnMsg("删除成功！");
        } else {
            return Res.returnMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAllAdvice(List<String> ids) {
        int i = bookAdviceMapper.deleteBatchIds(ids);
        if (i != -1) {
            return Res.returnMsg("批量删除成功");
        } else {
            return Res.returnMsg("批量删除失败");
        }
    }
}
